package com.linkhub.oms.admin.modules.product.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.linkhub.oms.admin.modules.product.domain.param.*;
import com.linkhub.oms.admin.modules.product.domain.vo.LocalProductDetailsVO;
import com.linkhub.oms.admin.modules.product.domain.vo.LocalProductExcelVO;
import com.linkhub.oms.admin.modules.product.domain.vo.LocalProductSearchVO;
import com.linkhub.oms.admin.modules.product.service.LocalProductBizService;
import com.linkhub.oms.admin.modules.product.service.LocalProductExcelService;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import com.linkhub.oms.common.response.PageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: LocalProductController
 * @Author: echo
 * @Date: 2024/3/20 11:09
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"本地商品管理模块"})
@RequestMapping("/local_product")
@SaCheckLogin
public class LocalProductController {

    @Resource
    private LocalProductBizService localProductBizService;
    @Resource
    private LocalProductExcelService localProductExcelService;

    @GetMapping("/search")
    @ApiOperation(value = "本地产品-包材搜索接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.STORE_STAFF}, mode = SaMode.OR)
    public DataResponse<PageDTO<LocalProductSearchVO>> pageSearchLocalProducts(@Validated LocalProductQueryParam localProductQueryParam) {
        return DataResponse.success(localProductBizService.pageSearchLocalProducts(localProductQueryParam));
    }

    @PostMapping
    @ApiOperation(value = "本地产品-包材新增接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    public DataResponse<Void> saveLocalProduct(@Validated @RequestBody LocalProductAddParam localProductAddParam) {
        localProductBizService.saveLocalProduct(localProductAddParam);
        return DataResponse.success();
    }

    @PutMapping("/{skuId}")
    @ApiOperation(value = "本地产品-包材编辑接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<Void> editLocalProduct(@PathVariable("skuId") String skuId, @RequestBody LocalProductEditParam localProductEditParam) {
        localProductBizService.editLocalProduct(skuId, localProductEditParam);
        return DataResponse.success();
    }

    @GetMapping("/supplier/{sysSupplierId}")
    @ApiOperation(value = "通过sysSupplierId查询本地商品接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE}, mode = SaMode.OR)
    public DataResponse<List<LocalProductSearchVO>> queryLocalProductBySupplierId(@Validated SupplierQueryLocalProductParam supplierQueryLocalProductParam) {
        return DataResponse.success(localProductBizService.queryLocalProductBySupplierId(supplierQueryLocalProductParam));
    }

    @GetMapping("/{skuId}/details")
    @ApiOperation(value = "本地产品-包材详情接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER, RoleConstant.EMPLOYEE, RoleConstant.STORE_STAFF}, mode = SaMode.OR)
    public DataResponse<LocalProductDetailsVO> localProductsDetails(@PathVariable("skuId") String skuId) {
        return DataResponse.success(localProductBizService.localProductsDetails(skuId));
    }

    @PostMapping("/excel/{productType}")
    @ApiOperation(value = "本地产品-包材excel导入接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    public DataResponse<LocalProductExcelVO> importExcel(@PathVariable("productType") Integer productType,
                                                         @RequestParam("file") MultipartFile file) {
        return DataResponse.success(localProductExcelService.importExcel(LhSessionContext.getUserId(), productType, file));
    }

    @PutMapping("/recheck/{skuId}")
    @ApiOperation(value = "本地产品-包材仓库复核接口")
    @SaCheckRole(value = {RoleConstant.STORE_STAFF})
    public DataResponse<Void> localProductRecheck(@PathVariable("skuId") String skuId, @Validated @RequestBody LocalProductRecheckParam localProductRecheckParam){
        localProductBizService.localProductRecheck(skuId,localProductRecheckParam);
        return DataResponse.success();
    }

    @GetMapping("/material/export")
    @ApiOperation(value = "本地包材excel导出接口")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE, RoleConstant.STORE_STAFF}, mode = SaMode.OR)
    public void exportExcel( HttpServletResponse response ) {
        localProductExcelService.exportPackagingMaterialsToCSV(response);
    }
}
